﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;

using Whoever.Framework.Enum;

namespace Whoever.Framework.Utils
{
    public static class Localized
    {
        public static string Action(string actionName)
        {
            if (string.IsNullOrEmpty(actionName))
                throw new Exception("The actionName parameter is empty");

            var actionDescription = string.Empty;

            var resourceBaseName = ConfigurationManager.AppSettings["ActionResourceBaseName"];
            var resourceAssembly = ConfigurationManager.AppSettings["ActionResourceAssembly"];

            if (!string.IsNullOrEmpty(resourceBaseName) && !string.IsNullOrEmpty(resourceAssembly))
            {
                var assembly = Assembly.Load(resourceAssembly);
                var resourceManager = new ResourceManager(resourceBaseName, assembly);
                actionDescription = resourceManager.GetString(actionName);
            }

            if (string.IsNullOrEmpty(actionDescription))
                actionDescription = actionName;

            return actionDescription;
        }

        public static string Text(string textName)
        {
            if (string.IsNullOrEmpty(textName))
                throw new Exception("The textName parameter is empty");

            var textDescription = string.Empty;

            var resourceBaseName = ConfigurationManager.AppSettings["TextResourceBaseName"];
            var resourceAssembly = ConfigurationManager.AppSettings["TextResourceAssembly"];

            if (!string.IsNullOrEmpty(resourceBaseName) && !string.IsNullOrEmpty(resourceAssembly))
            {
                var assembly = Assembly.Load(resourceAssembly);
                var resourceManager = new ResourceManager(resourceBaseName, assembly);
                textDescription = resourceManager.GetString(textName);
            }

            if (string.IsNullOrEmpty(textDescription))
                textDescription = textName;

            return textDescription;
        }

        public static string Message(string messageName, MessageType messageType)
        {
            if (string.IsNullOrEmpty(messageName))
                throw new Exception("The messageName parameter is empty");

            var messageDescription = string.Empty;

            var resourceBaseName = ConfigurationManager.AppSettings["MessageResourceBaseName"];
            var resourceAssembly = ConfigurationManager.AppSettings["MessageResourceAssembly"];

            if (!string.IsNullOrEmpty(resourceBaseName) && !string.IsNullOrEmpty(resourceAssembly))
            {
                var assembly = Assembly.Load(resourceAssembly);
                var resourceManager = new ResourceManager(resourceBaseName, assembly);
                messageDescription = resourceManager.GetString(string.Format("{0}.{1}", messageType.ToString(), messageName));
            }

            if (string.IsNullOrEmpty(messageDescription))
                messageDescription = string.Format("{0}.{1}", messageType.ToString(), messageName);

            return messageDescription;
        }
    }
}
